package com.yx.yinshanjinniu.qcloud;

import com.tencent.qcloud.core.auth.BasicLifecycleCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudLifecycleCredentials;
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
import com.tencent.qcloud.core.common.QCloudClientException;

public class MySessionCredentialProvider
        extends BasicLifecycleCredentialProvider {
    public String tmpSecretId;
    public String tmpSecretKey;
    public String sessionToken;
    public long startTime;
    public long endTime;

    public MySessionCredentialProvider(String tmpSecretId, String tmpSecretKey, String sessionToken, long startTime, long endTime){
        this.tmpSecretId = tmpSecretId;
        this.tmpSecretKey = tmpSecretKey;
        this.sessionToken = sessionToken;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    @Override
    protected QCloudLifecycleCredentials fetchNewCredentials()
            throws QCloudClientException {

        // 首先从您的临时密钥服务器获取包含了密钥信息的响应
        // 然后解析响应，获取临时密钥信息
//        tmpSecretId = "SECRETID"; // 临时密钥 SecretId
//        tmpSecretKey = "SECRETKEY"; // 临时密钥 SecretKey
//        sessionToken = "SESSIONTOKEN"; // 临时密钥 Token
//        long expiredTime = Long.parseLong(DataTimeUtils.getTimeStamp());//临时密钥有效截止时间戳，单位是秒


        //建议返回服务器时间作为签名的开始时间，避免由于用户手机本地时间偏差过大导致请求过期
        // 返回服务器时间作为签名的起始时间
//        long startTime = Long.parseLong(DataTimeUtils.getTimeStamp()); //临时密钥有效起始时间，单位是秒


        // 最后返回临时密钥信息对象
        return new SessionQCloudCredentials(tmpSecretId, tmpSecretKey,
                sessionToken, startTime, endTime);
    }

}
